﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using Crawler.Framework.Repositories;
using Crawler.IRepositories;
using Crawler.IRepositories.SystemDefault;
using Crawler.Framework.Analyzers;
using Crawler.Framework.EventBus;
using Crawler.Core.EventBus;
using Crawler.Framework;
using Microsoft.EntityFrameworkCore;
using Crawler.Common;
using Crawler.Framework.Repositories.SystemDefault;
using Crawler.IRepositories.DefaultApp;
using Crawler.Framework.Repositories.DefaultApp;
using SqlSugar;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class CrawlerService
    {
        public static IServiceCollection AddFrameworkServices(this IServiceCollection services)
        {
            services.AddScoped<ICollectorUrlRepository, CollectorUrlRepository>();
            services.AddScoped<IHttpConfigRepository, HttpConfigRepository>();
            services.AddScoped<IDefaultPageInfoRepository, DefaultPageInfoRepository>();
            services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
            services.AddSingleton< IEventBus, DefualtEventBus> ();
            return services;
        }

        public static IServiceCollection AddDefaultDbContext(this IServiceCollection services)
        {
            services.AddTransient(typeof(SqlSugarClient), provider =>
            {
                return new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = Constant.AppSetting.DefaultDataConstr,//必填, 数据库连接字符串
                    DbType = DbType.MySql,         //必填, 数据库类型
                    IsAutoCloseConnection = true,       //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
                    InitKeyType = InitKeyType.SystemTable    //默认SystemTable, 字段信息读取, 如：该属性是不是主键，是不是标识列等等信息
                });

            });
            return services;
        }

    }
}
